package com.phpxiu.app.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.phpxiu.app.adapter.RoomManagersAdapter;
import com.phpxiu.app.adapter.holder.RoomManagerViewHolder2;
import com.phpxiu.app.config.HttpConfig;
import com.phpxiu.app.kkylive.KKYApp;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.list.AdminListContent;
import com.phpxiu.app.model.response.AdminListModel;
import com.phpxiu.app.model.response.AdminModel;
import com.phpxiu.app.okhttp.OKHttp;
import com.phpxiu.app.okhttp.handler.OKHttpUIHandler;
import com.phpxiu.app.okhttp.param.OKHttpParam;
import com.phpxiu.app.utils.KKYUtil;
import com.phpxiu.app.view.custom.ListEmptyView;
import com.phpxiu.app.view.custom.WindowLayout;
import com.phpxiu.app.view.dialog.AdminListOperaMenus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 吴金洲 on 2016/7/8
 */
public class RoomManagers extends UIBase implements View.OnClickListener,AdminListOperaMenus.AdminListOperaCallBack,AdapterView.OnItemLongClickListener {
    public final static String EXTRA_PARAM_ROOM_ID="extra_param_room_id";
    public final static String TAG="RoomManagers";
    private String roomId;
    private OKHttpParam param=OKHttpParam.builder(false);
    private List<AdminListContent>managers=new ArrayList<AdminListContent>();
    //private SwipeListView mListView;
    private ListView mListView;
    private ListEmptyView emptyView;
    private RoomManagersAdapter mAdapter;
    private WindowLayout window;
    private AdminListOperaMenus operaMenus;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        roomId=getIntent().getStringExtra(EXTRA_PARAM_ROOM_ID);
        if(roomId==null||"".equals(roomId)){
            finish();
            return;
        }
        setContentView(R.layout.room_manager);
        window=(WindowLayout)findViewById(R.id.window_view);
        findViewById(R.id.top_bar_back_btn).setOnClickListener(this);
        init();
    }

    private void  init(){
        mListView=(ListView)findViewById(R.id.managers_list_view);
        mListView.setOnItemLongClickListener(this);
        //mListView.setRightViewWidth(Util.dip2px(this,100));
        emptyView=new ListEmptyView(this);
        emptyView.setMsg(getString(R.string.list_empty_title));
        mListView.setEmptyView(emptyView);
        mListView.setDivider(null);
        mAdapter=new RoomManagersAdapter(this,managers);
        mListView.setAdapter(mAdapter);
        operaMenus=new AdminListOperaMenus(this);
        operaMenus.getWindow().getAttributes().width=(int)KKYApp.screenWidth;
        operaMenus.setOperaCallBack(this);
        request();
    }

    private void request(){
        OKHttp.post(HttpConfig.API_GET_ADMIN_LIST, param.jsonParam(), TAG, new OKHttpUIHandler(AdminListModel.class) {
            @Override
            public void onSuccess(Object model) {
                try {
                    emptyView.loaded();
                    AdminListModel m = (AdminListModel) model;
                    KKYUtil.log("管理员列表数据：" + m.getResponseStr());
                    managers.clear();
                    managers.addAll(m.getData());
                    mAdapter.notifyDataSetChanged();
                    window.showTopMsg(getString(R.string.long_click_delete));
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErr(String errMsg) {
                KKYUtil.log("管理员列表请求失败：" + errMsg);
            }
        });
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        if(view.getTag()!=null&&view.getTag() instanceof RoomManagerViewHolder2){
            RoomManagerViewHolder2 holder=(RoomManagerViewHolder2)view.getTag();
            operaMenus.show(holder.getUid(),holder.getPosition());
        }
        return true;//拦截其它动作
    }

    @Override
    public void onCancelAdmin(String uid,int position) {
        cancelAdmin(uid);
        try {
            managers.remove(position);
            mAdapter.notifyDataSetChanged();
        }catch (IndexOutOfBoundsException e){

        }
    }

    /**
     * 取消管理员
     * @param uid
     */
    public void cancelAdmin(String uid){
        OKHttpParam cancelParam=OKHttpParam.builder(false);
        cancelParam.put("uid", uid);
        cancelParam.remove("platform");
        OKHttp.post(HttpConfig.API_CANCEL_ADMIN,cancelParam.jsonParam(),TAG, new OKHttpUIHandler(AdminModel.class) {
            @Override
            public void onSuccess(Object model) {
                window.showTopMsg(getString(R.string.ui_manager_delete_success));
            }

            @Override
            public void onErr(String errMsg) {
                window.showTopMsg(errMsg);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.top_bar_back_btn:
                onBackPressed();
                break;
            default:
                break;
        }
    }

    @Override
    public void finish() {
        if(operaMenus!=null){
            operaMenus.dismiss();
        }
        super.finish();
    }
}